package com.ys.malini.web.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.IShowroomDao;
import com.ys.malini.model.TShowroominfo;
import com.ys.malini.util.Constant;
import com.ys.malini.web.form.RoomForm;

public class ShowroomAction extends DispatchAction {
	private IShowroomDao showroomDao;


	
	public IShowroomDao getShowroomDao() {
		return showroomDao;
	}


	public void setShowroomDao(IShowroomDao showroomDao) {
		this.showroomDao = showroomDao;
	}


	/**
	 * 跳到查询页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("------toquery-------");
		List<TShowroominfo> roomlist = showroomDao.query(null,true);
		request.setAttribute("roomlist", roomlist);
		return mapping.findForward("listPage");
	}



	/**
	 * 展厅信息查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoomForm formbean = (RoomForm)form;
		List<TShowroominfo> roomlist = showroomDao.query(formbean,true);
		request.setAttribute("roomlist", roomlist);
		return mapping.findForward("listPage");
	}
	
	
	

	/**
	 * 跳到更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String roomId = request.getParameter("roomId");
		
		TShowroominfo roominfo = showroomDao.queryRoomInfoById(roomId);
		request.setAttribute("roominfo", roominfo);
		
		
		return mapping.findForward("update");
	}

	/**
	 * 更新页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoomForm formbean =(RoomForm)form;
		String message = showroomDao.updateRoomInfoById(formbean);
		request.setAttribute("message",message);
		return mapping.findForward("update");
	}

	/**
	 * 跳到添加页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	/**
	 * 展厅信息更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoomForm formbean = (RoomForm) form;
		String path=request.getRealPath("/") + Constant.pictureDirectory+ File.separator+"zt"+File.separator;
		formbean.setPath(path);
		String message = showroomDao.addRoomInfo(formbean);
		
		request.setAttribute("message", message);
		return mapping.findForward("add");
	}
	
	
	/**
	 * 展厅信息删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String roomId=request.getParameter("roomId");
		Map map = new HashMap();
		map.put("roomId", roomId);
		String message = showroomDao.deleteRoomInfo(map);
		request.setAttribute("message", message);
		List<TShowroominfo> roomlist = showroomDao.query(null,true);
		request.setAttribute("roomlist", roomlist);
		return mapping.findForward("listPage");
	}
	
	
	/**
	 * 跳到前台展示
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("------toquery-------");
		List<TShowroominfo> roomlist = showroomDao.query(null,true);
		request.setAttribute("roomlist", roomlist);
		return mapping.findForward("show");
	}
	
	
}
